package org.wzq.test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import org.wzq.base.DeviceInfo;
import org.wzq.mdm.R;
import org.wzq.utils.ConvertUtils;
import org.wzq.utils.Logger;
import org.wzq.utils.socket.SocketManager;
import org.wzq.utils.socket.SocketMsgHandler;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * project:
 * author: wzq
 * date: 2014/8/9
 * description:
 */
public class ActTestSocket extends Activity {
    private Logger logger = Logger.newLogger(true, ActTestSocket.class);
    private TextView tvTime;
    private TextView tvInfo;
    private EditText etSend;
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    SocketManager socketManager = SocketManager.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_test_socket);
        tvTime = (TextView) findViewById(R.id.tv_receive_time);
        tvInfo = (TextView) findViewById(R.id.tv_receive_info);
        etSend = (EditText) findViewById(R.id.et_socket_send);

        socketManager.setSocketMsgHandler(new SocketMsgHandler() {
            @Override
            public void onReceiveMsg(byte[] msg) {
                String str = ConvertUtils.bytes2String(msg);
                handleView(str);
                if ("require send info".equals(str)) { //send msg
/*                    {
                            token: "xxx",
                            deviceUuid: "xxx"
                    }*/
                    JSONObject json = new JSONObject();
                    try {
                        json.put("token", "test-token");
                        json.put("deviceUuid", DeviceInfo.getDeviceUuid());
                        String data = json.toString();
                        socketManager.sendMsg(data);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if ("tick".equals(str)) { //tick
                    logger.v("receive tick");
                } else {
                    logger.e("unknown msg from server:" + str);
                }
            }
        });

        findViewById(R.id.btn_start).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                socketManager.start();
            }
        });

        findViewById(R.id.btn_stop).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                socketManager.stop();
            }
        });

        findViewById(R.id.btn_send_msg).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //byte[] msg = {(byte) 0x01};
                String msg = etSend.getText().toString();
                socketManager.sendMsg(msg);
            }
        });
    }

    void handleView(final String info) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                tvTime.setText(sdf.format(new Date()));
                tvInfo.setText(info);
            }
        });
    }
}
